package com.sinoglobal.ningxia.qr;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.utils.SharedPreferenceUtil;
import com.sinoglobal.ningxia.widget.MyDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author limeishu
 * @createdate 2014-8-13 下午2:47:27
 * @Description: TODO(扫描历史记录页面)
 */
public class HistoryCodeActivity extends BaseActivity {
	private ListView history;// 历史记录列表
	private TextView noData;
	private HistoryListAdapter adapter;
	private HistoryListVo historyMessage;// 历史记录列表bean类
	private MyDialog mDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_qr_history_code);
		openChenJInShi();//开启沉浸式
		titleView.setText(getString(R.string.qr_history_title));
		microButtonRight.setVisibility(View.VISIBLE);
		microButtonRight.setImageResource(R.drawable.qr_icon_history_del);

	}

	@Override
	public void onResume() {
		adapter = new HistoryListAdapter(HistoryCodeActivity.this);
		init();
		getHistoryData();
		setOnClickListener();

		super.onResume();
	}

	private void setOnClickListener() {
		// 点击清空历史记录
		microButtonRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				mDialog = new MyDialog(HistoryCodeActivity.this);
				mDialog.setMessage(getString(R.string.qr_history_dialog_message));
				mDialog.setTitle(getString(R.string.qr_history_title));
				mDialog.setPositiveButton(getString(R.string.qr_history_btn_clean));
				mDialog.setNegativeButton(getString(R.string.btn_cancle));
				mDialog.setMyDialogListener(new MyDialog.MyDialogListener() {

					@Override
					public void doPositive() {
						deleteHistory();
					}

					@Override
					public void doNegative() {

					}
				});
				mDialog.show();

			}
		});
		history.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				Intent intent = new Intent(HistoryCodeActivity.this,
						QrStrResultActivity.class);
				intent.putExtra("captureResult",
						adapter.getHistoryList().get(position).getContent());
				startActivity(intent);
			}
		});
	}

	private void init() {
		noData = (TextView) findViewById(R.id.no_data);
		history = (ListView) findViewById(R.id.history_list);
		history.setAdapter(adapter);

	}

	// 清空历史记录
	private void deleteHistory() {
		SharedPreferenceUtil.removeString(getApplicationContext(),
				"historyListStr");
		adapter = new HistoryListAdapter(HistoryCodeActivity.this);
		init();
		historyMessage = new HistoryListVo();
		setListView(historyMessage);
	}

	// 加载ListView数据
	private void setListView(HistoryListVo historyMessage) {
		List<HistoryMessageVo> historyList = historyMessage.getJson();
		if (historyList != null && !historyList.isEmpty()) {
			// 取数据的前50条
			if (historyList.size() <= 50) {
				adapter.setHistoryList(historyList);
			} else {
				List<HistoryMessageVo> historyListLimit = new ArrayList<HistoryMessageVo>();
				for (int i = 0; i < 50; i++) {
					historyListLimit.add(historyList.get(i));
				}
				adapter.setHistoryList(historyListLimit);
			}
		} else {
			microButtonRight.setVisibility(View.GONE);
			noData.setVisibility(View.VISIBLE);
		}
	}

	// 获取历史数据， 历史数据不为空则展示，为空提示暂无历史记录
	private void getHistoryData() {
		try {
			String data = SharedPreferenceUtil.getString(
					getApplicationContext(), "historyListStr");
			if (data != null && data.length() > 0) {
				historyMessage = JSON.parseObject(data, HistoryListVo.class);
				setListView(historyMessage);
			} else {
				microButtonRight.setVisibility(View.GONE);
				noData.setVisibility(View.VISIBLE);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
